package notepad;

import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.InputEvent;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;

import javax.swing.ImageIcon;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JTextArea;
import javax.swing.KeyStroke;

public class NotePad extends JFrame implements ActionListener {
	JTextArea jta;
	JMenuBar jmb;
	JMenu jm;
	JMenuItem jmi1, jmi2, jmi3;
	File file;

	public NotePad() {
		jta = new JTextArea();
		jmb = new JMenuBar();
		jm = new JMenu("文件(F)");
		jm.setMnemonic('F');
		ImageIcon icon = new ImageIcon("images/pic1.jpg");
		icon.setImage(icon.getImage().getScaledInstance(20, 20, Image.SCALE_DEFAULT));
		jmi1 = new JMenuItem("打开", icon);
		jmi1.addActionListener(this);
		jmi1.setActionCommand("open");
		jmi1.setAccelerator(KeyStroke.getKeyStroke('O', InputEvent.CTRL_MASK));
		jmi2 = new JMenuItem("保存");
		jmi2.addActionListener(this);
		jmi2.setActionCommand("save");
		jmi2.setAccelerator(KeyStroke.getKeyStroke('S', InputEvent.CTRL_MASK));
		jmi3 = new JMenuItem("另存为");
		jmi3.addActionListener(this);
		jmi3.setActionCommand("otherSave");
		this.setJMenuBar(jmb);
		jmb.add(jm);
		jm.add(jmi1);
		jm.add(jmi2);
		jm.add(jmi3);
		this.add(jta);
		this.setTitle("记事本");
		this.setSize(400, 300);
		this.setVisible(true);
		this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
	}

	public static void main(String[] args) {
		NotePad notePad = new NotePad();

	}

	public String readFile(File file) {
		FileReader fr = null;
		BufferedReader br = null;
		String s = "";
		String context = "";
		try {
			fr = new FileReader(file);
			br = new BufferedReader(fr);
			while ((s = br.readLine()) != null) {
				context += s + "\r\n";
			}
		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				br.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				fr.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
		return context;

	}

	public void writeFile(File file, String content) {
		FileWriter fw = null;
		BufferedWriter bw = null;

		try {
			fw = new FileWriter(file);
			bw = new BufferedWriter(fw);
			bw.write(content);

		} catch (Exception e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			try {
				bw.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
			try {
				fw.close();
			} catch (IOException e1) {
				// TODO Auto-generated catch block
				e1.printStackTrace();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getActionCommand().equals("open")) {
			JFileChooser jfc1 = new JFileChooser();
			jfc1.setDialogTitle("请选择文件");
			jfc1.showSaveDialog(null);
			jfc1.setVisible(true);
			file = jfc1.getSelectedFile();
			jta.setText(readFile(file));
		} else if (e.getActionCommand().equals("save")) {
			writeFile(file, jta.getText());
		} else if (e.getActionCommand().equals("otherSave")) {
			JFileChooser jfc1 = new JFileChooser();
			jfc1.setDialogTitle("另存为");
			jfc1.showSaveDialog(null);
			jfc1.setVisible(true);
			File file = jfc1.getSelectedFile();
			writeFile(file, jta.getText());

		}

	}

}
